﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using CPPEI.ETL.Domain.WcfServices;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 登录
    /// </summary>
    [CPPEI.WebApi.PostAction("SYS", "Login", "1.0")]
    public class LoginAction : BaseAction<LoginParams, LoginResult>
    {
        protected override ActionResult<LoginResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            LoginParams opParams)
        {
            var value = HttpContext.Current.Request.Cookies["SESSIONCOOKIES_SSO"];
            if (value != null && !string.IsNullOrEmpty(value.Value))
            {
                try
                {
                    var userinfo = new ServiceExtendService().GetUserByOnline(value["UserID"]);
                    opParams.Account = userinfo.UserName;
                    opParams.Password = userinfo.Pwd;
                }
                catch (Exception)
                {
                }
             
            }
            LoginResult result = new LoginResult();
            result.UserId = opParams.Account;
            ServiceResult<string> serResult = new SER.AccountService().Login(opParams.Account, opParams.Password);
            if (serResult.IsSuccess)
            {
                Intergration.Config.AuthProvider.Save(result.UserId, TimeSpan.FromHours(1));
                return new ActionResult<LoginResult>(result);
            }
            return new ActionResult<LoginResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
        }
    }

    public class LoginParams
    {
        /// <summary>
        /// 账号
        /// </summary>
        public string Account;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
    }

    public class LoginResult
    {
        public string UserId { get; set; }
    }
}
